package detplagiasi;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import org.apache.tika.Tika;

public class Container {
    static String addd;

  public void address(String add){
      //String addd;
      addd = add;
      
  }
  
  public static String getAddress() {
      return addd;
  }
  
  public static void simpanFile(File file) throws Exception {

    Tika tika = new Tika();
    
    //String text = tika.parseToString(new File(file));
    BufferedReader inputStream = null;
    PrintWriter outputStream = null;

    String inFName = file.getCanonicalPath();
    String fName = file.getName().toLowerCase();
    //create new file, name=output.txt
    
    File outFName = null;
    
    if (inFName.endsWith(".docx")) {
        outFName = new File(addd + "\\" + fName.replace(".docx", ".txt"));
    }
    else if(inFName.endsWith(".pdf")){                    
        outFName = new File(addd + "\\" + fName.replace(".pdf",".txt"));
    }
    else if (inFName.endsWith(".doc")) {
        outFName = new File(addd + "\\" + fName.replace(".doc", ".txt"));
    }
    else if (inFName.endsWith(".pptx")) {
        outFName = new File(addd + "\\" + fName.replace(".pptx", ".txt"));
    }
        

    try {
      inputStream = new BufferedReader(tika.parse(new File(inFName)));
      outputStream = new PrintWriter(new FileWriter(outFName));

      String line;
      int lineCtr = 0;
      int lenStr = 0;
      while ((line = inputStream.readLine()) != null) {
        outputStream.println(line);
        lenStr += line.length();
        lineCtr++;
        outputStream.flush();
      }
      System.out.println("\t>> DEBUG: " + outFName + ": " + lenStr);
      System.out.println("\t>> DEBUG: " + lineCtr + " lines were saved.");
      System.out.println("\t>> DEBUG: " + outFName);

    }
    
    finally {
      if (inputStream != null) {
          inputStream.close(); 
          
      }
    }
          

   
    //System.out.print(text);
}}

